/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：UserCenterAction.java
 * History: Feb 18, 2013: Initially created, liuzhi.
 */
package com.xteam.ecsystem.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tangkf.utils.CommonUtils;
import com.tangkf.utils.file.FileUtils;
import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.entities.BaseDistributeRegion;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.entities.BaseMemberInfo;
import com.xteam.ecsystem.entities.BaseOrderGoods;
import com.xteam.ecsystem.entities.BaseOrderInfo;
import com.xteam.ecsystem.entities.BaseReceiveAddress;
import com.xteam.ecsystem.entities.BaseUserMessage;
import com.xteam.ecsystem.service.front.FrontGoodsAttrService;
import com.xteam.ecsystem.service.front.FrontGoodsCollectionService;
import com.xteam.ecsystem.service.front.FrontOrderService;
import com.xteam.ecsystem.service.front.FrontUserAddrService;
import com.xteam.ecsystem.service.front.FrontUserCommentService;
import com.xteam.ecsystem.service.front.FrontUserMessageService;
import com.xteam.ecsystem.service.front.FrontUserService;
import com.xteam.ecsystem.system.ConfigManager;
import com.xteam.ecsystem.utils.AppConfig;
import com.xteam.ecsystem.utils.FrontUtil;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 前端会员中心用户请求控制层
 * @author liuzhi
 */
@Controller
public class UserCenterAction {
	
	// 过滤资源路径，防止非登录用户非法访问。
	public static final String USER_INDEX = "/user/index.xhtml";
	public static final String USER_USERINFO = "/user/userInfo.xhtml";
	public static final String USER_EDITUSERINFO = "/user/editUserInfo.xhtml";
	public static final String USER_EDITUSERINFOSUCC = "/user/editUserInfoSucc.xhtml";
	public static final String USER_EDITUSERPWD = "/user/editUserPwd.xhtml";
	public static final String USER_EDITUSERPWDSUCC = "/user/editUserPwdSucc.xhtml";
	
	public static final String USER_EDITUSERPWDFAIL = "/user/editUserPwdFail.xhtml";
	public static final String USER_USERADDR = "/user/userAddr.xhtml";
	public static final String USER_SAVEUSERADDR = "/user/saveUserAddr.xhtml";
	public static final String USER_UPDATEUSERDEFADDR = "/user/updateUserDefAddr.xhtml";
	public static final String USER_DELETEUSERADDR = "/user/deleteUserAddr.xhtml";
	public static final String USER_GETUSERADDR = "/user/getUserAddr.xhtml";
	
	public static final String USER_USERFAV = "/user/userFav.xhtml";
	public static final String USER_REMOVEFROMFAV = "/user/removeFromFav.xhtml";
	public static final String USER_USERMSG = "/user/userMsg.xhtml";
	public static final String USER_SAVEUSERMSG = "/user/saveUserMsg.xhtml";
	public static final String USER_DELUSERMSG = "/user/delUserMsg.xhtml";
	public static final String USER_USERCMTS = "/user/userCmts.xhtml";
	
	public static final String USER_REMOVEFROMUSERCMTS = "/user/removeFromUserCmts.xhtml";
	public static final String USER_ORDER = "/user/order.xhtml";
	public static final String USER_CANCELORDER = "/user/cancelOrder.xhtml";
	public static final String USER_MERGEORDER = "/user/mergeOrder.xhtml";
	public static final String USER_ORDERDETAIL = "/user/orderDetail.xhtml";
	public static final String USER_UPDATECONSIGNEE = "/user/updateConsignee.xhtml";
	
	// 获得商品属性不便于拦截，以免影响未登录用户购买商品
	public static final String USER_GETGOODSATTRLIST = "/user/getGoodsAttrList.xhtml";
	
	/**
	 * 匹配当前资源路径是否包含过滤资源路径
	 * @author liuzhi Mar 28, 2013
	 * @param req
	 * @return
	 */
	public static boolean isFilterUrl(String reqUrl) {
		if(StringUtil.isNotNullOrEmpty(reqUrl)){
			if(reqUrl.contains(USER_INDEX) || reqUrl.contains(USER_USERINFO)
					|| reqUrl.contains(USER_EDITUSERINFO) || reqUrl.contains(USER_EDITUSERINFOSUCC)
					|| reqUrl.contains(USER_EDITUSERPWD) || reqUrl.contains(USER_EDITUSERPWDSUCC)
					|| reqUrl.contains(USER_EDITUSERPWDFAIL) || reqUrl.contains(USER_USERADDR)
					|| reqUrl.contains(USER_SAVEUSERADDR) || reqUrl.contains(USER_UPDATEUSERDEFADDR)
					|| reqUrl.contains(USER_DELETEUSERADDR) || reqUrl.contains(USER_GETUSERADDR)
					|| reqUrl.contains(USER_USERFAV) || reqUrl.contains(USER_REMOVEFROMFAV)
					|| reqUrl.contains(USER_USERMSG) || reqUrl.contains(USER_SAVEUSERMSG)
					|| reqUrl.contains(USER_DELUSERMSG) || reqUrl.contains(USER_USERCMTS)
					|| reqUrl.contains(USER_REMOVEFROMUSERCMTS) || reqUrl.contains(USER_ORDER) 
					|| reqUrl.contains(USER_CANCELORDER) || reqUrl.contains(USER_MERGEORDER) 
					|| reqUrl.contains(USER_ORDERDETAIL) || reqUrl.contains(USER_UPDATECONSIGNEE)){
				return true;
			}
		}
		
		return false;
	}
	
	
	@Autowired
	private FrontUserService userService;
	
	@Autowired
	private FrontGoodsCollectionService gcService;
	
	@Autowired
	private FrontUserAddrService uaService;
	
	@Autowired
	private FrontUserCommentService ucService;
	
	@Autowired
	private FrontUserMessageService umService;
	
	@Autowired
	private FrontGoodsAttrService fgaService;
	
	@Autowired
	private FrontOrderService foService;
	
	@Autowired
	private ConfigManager cfgManager;
	
	/**
	 * 设置响应类型
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 */
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	}
	
	/**
	 * 设置请求和响应编码
	 * @author liuzhi Mar 22, 2013
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void setCharacterEncoding(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
	};
	
	/**
	 * 前往会员中心-欢迎页
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/index.xhtml", method = RequestMethod.GET)
	public String userIndex(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 前往会员中心-用户信息
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/userInfo.xhtml", method = RequestMethod.GET)
	public String userInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 编辑用户基本信息
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/user/editUserInfo.xhtml", method = RequestMethod.POST)
	public void editUserInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String status = "succ";
		String bmbEmail = FrontUtil.getStringParam(request, "bmbEmail", "");
		String bmbMobile = FrontUtil.getStringParam(request, "bmbMobile", "");
		String bmbQuestion = FrontUtil.getStringParam(request, "bmbQuestion", "");
		String bmbAnswer = FrontUtil.getStringParam(request, "bmbAnswer", "");
		int bmbSmnotify = FrontUtil.getIntegerParam(request, "bmbSmnotify", 0);
		int bmbEmnotify = FrontUtil.getIntegerParam(request, "bmbEmnotify", 0);
		String dataArray = FrontUtil.getStringParam(request, "dataArray", "");
		JSONArray jsonArray = null;
		
		if(dataArray != null && dataArray != ""){
			jsonArray = JSONArray.fromObject(dataArray);
		}
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			sessionUser.setBmbEmail(bmbEmail);
			sessionUser.setBmbMobile(bmbMobile);
			sessionUser.setBmbQuestion(bmbQuestion);
			sessionUser.setBmbAnswer(bmbAnswer);
			sessionUser.setBmbSmnotify(bmbSmnotify);
			sessionUser.setBmbEmnotify(bmbEmnotify);
			
			// 更新会员信息
			String bmbId = this.userService.saveOrUpdateBMB(sessionUser, sessionUser.getBmbId());
			
			// 剔除会员资料信息以便重新保存
			this.userService.delBmiByMemberId(bmbId);
			
			if(jsonArray != null && jsonArray.size() > 0){
				BaseMemberInfo bmiObj = null;
				List listObj = new ArrayList();
				
				for (int i = 0; i < jsonArray.size(); i++) {
					JSONObject jsonObj = jsonArray.getJSONObject(i);
					
					bmiObj = new BaseMemberInfo();
					bmiObj.setBmiMember(bmbId);
					bmiObj.setBmiItem(jsonObj.getString("bmiItem"));
					bmiObj.setBmiValue(jsonObj.getString("bmiValue"));
					
					listObj.add(bmiObj);
				}
				
				// 保存会员资料项信息
				this.userService.saveMuchObj(listObj);
			}
		}else{
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 前往修改用户资料成功页面
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/editUserInfoSucc.xhtml", method = RequestMethod.GET)
	public String editUserInfoSucc(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 修改用户密码
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/editUserPwd.xhtml", method = RequestMethod.POST)
	public void editUserPwd(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status = "succ";
		String oldPwd = FrontUtil.getStringParam(request, "oldPwd", "");
		String newPwd = FrontUtil.getStringParam(request, "newPwd", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			BaseMemberBase bmbObj = this.userService.getBMBByEmailAPwd(sessionUser.getBmbEmail(), oldPwd);
			if(bmbObj != null){
				bmbObj.setBmbPassword(newPwd);
				this.userService.saveOrUpdateBMB(bmbObj, bmbObj.getBmbId());
			}else{
				status = "fail";
			}
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 前往会员中心-修改密码成功页面
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/editUserPwdSucc.xhtml", method = RequestMethod.GET)
	public String editUserPwdSucc(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			request.getSession().removeAttribute(Constant.VAR_MEMBER.value());
			request.getSession().invalidate();
		}
		
		return template;
	}
	
	/**
	 * 前往会员中心-修改密码失败页面
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/editUserPwdFail.xhtml", method = RequestMethod.GET)
	public String editUserPwdFail(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 前往会员中心-我的订单
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/order.xhtml", method = RequestMethod.GET)
	public String order(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			//设置数据
			model.put(Constant.VAR_PAGEINDEX.value(), FrontUtil.getIntegerParam(request, "index", 1));
			model.put(Constant.VAR_PAGESIZE.value(), this.cfgManager.getSystemConfig().getSscOlpagesize());
		}
		
		return template;
	}
	
	/**
	 * 用户中心- 取消订单
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/cancelOrder.xhtml", method = RequestMethod.POST)
	public void cancelOrder(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status = "succ";
		String orderId = FrontUtil.getStringParam(request, "orderId", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			this.foService.saveCancelOrder(orderId, sessionUser.getBmbNick(), request.getLocalAddr());
		}else{
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	@RequestMapping(value = "/user/mergeOrder.xhtml", method = RequestMethod.POST)
	public void mergeOrder(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status = "fail";
		String mainOrder = FrontUtil.getStringParam(request, "mainOrder", "");
		String fellowOrder = FrontUtil.getStringParam(request, "fellowOrder", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			BaseOrderInfo mainOrderInfo = this.foService.getOrderInfo(mainOrder);
			BaseOrderInfo fellowOrderInfo = this.foService.getOrderInfo(fellowOrder);
			
			if(mainOrderInfo != null && fellowOrderInfo != null){
				if((mainOrderInfo.getBoiOrder()).equals(fellowOrderInfo.getBoiOrder())
						&& (mainOrderInfo.getBoiPay()).equals(fellowOrderInfo.getBoiPay())
						&& (mainOrderInfo.getBoiShip()).equals(fellowOrderInfo.getBoiShip())){
					
					if(this.foService.saveMergeOrders(mainOrderInfo, fellowOrderInfo)){
						status = "succ";
					}
				}else{
					status = "unMergedOrder";
				}
			}else{
				status = "dataError";
			}
		
		}else{
			status = "sessionInvalid";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 前往会员中心-收货地址
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/user/orderDetail.xhtml", method = RequestMethod.GET)
	public String orderDetail(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			// 设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
			if (sessionUser != null) {
				String orderId = FrontUtil.getStringParam(request, "orderId", "");

				BaseOrderInfo boiObj = this.foService.getOrderInfo(orderId);
				List<BaseOrderGoods> bogList = (List<BaseOrderGoods>)this.foService.getOrderGoods(orderId);
				
				if(bogList == null) bogList = new ArrayList<BaseOrderGoods>();
				
				// 存储订单信息
				model.put(Constant.VAR_ORDERINFO.value(), boiObj);
				// 存储订单详情
				model.put(Constant.VAR_ORDERGOODS.value(), bogList);
			}
		}
		
		return template;
	}
	
	/**
	 * 订单详情中 - 修改收货人信息
	 * @author liuzhi Apr 3, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/updateConsignee.xhtml", method = RequestMethod.POST)
	public void updateConsignee(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		setToBeJsonType(request, response);
		
		String status = "";
		String boiId = FrontUtil.getStringParam(request, "boiId", "");
		String boiConsignee = FrontUtil.getStringParam(request, "boiConsignee", "");
		String boiZipcode = FrontUtil.getStringParam(request, "boiZipcode", "");
		String boiDetail = FrontUtil.getStringParam(request, "boiDetail", "");
		String boiPhone = FrontUtil.getStringParam(request, "boiPhone", "");
		String boiMobile = FrontUtil.getStringParam(request, "boiMobile", "");
		String boiBest = FrontUtil.getStringParam(request, "boiBest", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if(sessionUser != null){
			BaseOrderInfo boiObj = this.foService.getOrderInfo(boiId);
			if (boiObj != null) {
				boiObj.setBoiConsignee(boiConsignee);
				boiObj.setBoiZipcode(boiZipcode);
				boiObj.setBoiDetail(boiDetail);
				boiObj.setBoiPhone(boiPhone);
				boiObj.setBoiMobile(boiMobile);
				boiObj.setBoiBest(boiBest);
				
				this.foService.saveOrderConsignee(boiObj);
				status = "succ";
			}
		}else{
			status = "fail";
		}
		
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("status", status);
		jsonObj.put("orderId", boiId);
		
		response.getWriter().print(jsonObj);
	}
	
	/**
	 * 前往会员中心-收货地址
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/userAddr.xhtml", method = RequestMethod.GET)
	public String userAddr(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			// 设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			// 设置用户收货地址数目限定数量
			model.put(Constant.VAR_USERADDRLIMITNUM.value(), this.cfgManager.getSystemConfig().getSscUseraddrnum());
			
			BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
			if (sessionUser != null) {
				// 获取并设置当前用户收货地址数量
				model.put(Constant.VAR_USERADDRCURRENTNUM.value(), this.uaService.getUserAddrNums(sessionUser.getBmbId()));
			}
		}
		
		return template;
	}
	
	/**
	 * 保存用户收货地址
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/saveUserAddr.xhtml", method = RequestMethod.POST)
	public void saveUserAddr(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status = "succ";
		// 获取隐藏域中的收货地址id
		String braId = FrontUtil.getStringParam(request, "braId", "");
		String braConsignee = FrontUtil.getStringParam(request, "braConsignee", "");
		String braCountry = FrontUtil.getStringParam(request, "braCountry", "");
		String braProvince = FrontUtil.getStringParam(request, "braProvince", "");
		String braCity = FrontUtil.getStringParam(request, "braCity", "");
		String braDistrict = FrontUtil.getStringParam(request, "braDistrict", "");
		String braDetail = FrontUtil.getStringParam(request, "braDetail", "");
		String braZipcode = FrontUtil.getStringParam(request, "braZipcode", "");
		String braMobile = FrontUtil.getStringParam(request, "braMobile", "");
		String braPhone = FrontUtil.getStringParam(request, "braPhone", "");
		String braTime = FrontUtil.getStringParam(request, "braTime", "");
		int braIsdefault = FrontUtil.getIntegerParam(request, "braIsdefault", 0);
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			if (braIsdefault == 1) {
				// 取消之前保存的默认收货地址
				this.uaService.saveCancelUserDefAddr(sessionUser.getBmbId());
			}
			
			BaseReceiveAddress bra;
			
			if (StringUtil.isNotNullOrEmpty(braId)) {
				bra = this.uaService.getReceiverInfoById(braId);
			}else{
				bra = new BaseReceiveAddress();
			}
			
			bra.setBraMember(sessionUser.getBmbId());
			bra.setBraCountry(braCountry);
			bra.setBraProvince(braProvince);
			bra.setBraCity(braCity);
			bra.setBraDistrict(braDistrict);
			bra.setBraConsignee(braConsignee);
			bra.setBraDetail(braDetail);
			bra.setBraZipcode(braZipcode);
			bra.setBraMobile(braMobile);
			bra.setBraPhone(braPhone);
			bra.setBraTime(braTime);
			bra.setBraIsdefault(braIsdefault);
			
			this.uaService.saveOrUpdateObj(bra, braId);
		}else{
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 更改用户默认收货地址
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/updateUserDefAddr.xhtml", method = RequestMethod.POST)
	public void updateUserDefAddr(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status = "succ";
		String addrId = FrontUtil.getStringParam(request, "addrId", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			this.uaService.saveCancelUserDefAddr(sessionUser.getBmbId());
			this.uaService.saveUserDefAddr(addrId);
		}else{
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 删除用户收货地址
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/deleteUserAddr.xhtml", method = RequestMethod.POST)
	public void deleteUserAddr(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status = "succ";
		String addrId = FrontUtil.getStringParam(request, "addrId", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			this.uaService.deleteUserAddr(addrId);
		}else{
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 获得用户收货地址列表
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getUserAddr.xhtml", method = RequestMethod.POST)
	public void getUserAddr(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		setToBeJsonType(request, response);
		
		String status = "succ";
		String braStr = "";
		String addrId = FrontUtil.getStringParam(request, "addrId", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			BaseReceiveAddress bra = this.uaService.getReceiverInfoById(addrId);
			
			if(bra != null){
				braStr = JSONObject.fromObject(bra).toString();
			}
		}else{
			status = "fail";
		}
		
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("status", status);
		jsonObj.put("braJson", braStr);
		
		response.getWriter().print(jsonObj.toString());
	}
	
	/**
	 * 获得区域列表
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getRegionList.xhtml", method = RequestMethod.POST)
	public void getRegionList(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		setToBeJsonType(request, response);
		
		String bdrParent = FrontUtil.getStringParam(request, "bdrParent", "");
		
		List<BaseDistributeRegion> bdrList = this.uaService.getRegionList(null, bdrParent);
		
		if (bdrList != null && bdrList.size() > 0) {
			response.getWriter().write(JSONArray.fromObject(bdrList).toString());
		}else{
			response.getWriter().write("");
		}
	}
	
	/**
	 * 前往会员中心-我的收藏
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/userFav.xhtml", method = RequestMethod.GET)
	public String userFav(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			//设置数据
			model.put(Constant.VAR_PAGEINDEX.value(), FrontUtil.getIntegerParam(request, "index", 1));
			model.put(Constant.VAR_PAGESIZE.value(), this.cfgManager.getSystemConfig().getSscFlpagesize());
		}
		
		return template;
	}
	
	/**
	 * 从收藏列表中删除某收藏项
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/removeFromFav.xhtml", method = RequestMethod.POST)
	public void removeFromFav(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status = "succ";
		String goodsId = FrontUtil.getStringParam(request, "goodsId", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			this.gcService.delFromFav(sessionUser.getBmbId(), goodsId);
		}else{
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 前往会员中心-我的留言
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/userMsg.xhtml", method = RequestMethod.GET)
	public String userMsg(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			//设置数据
			model.put(Constant.VAR_PAGEINDEX.value(), FrontUtil.getIntegerParam(request, "index", 1));
			model.put(Constant.VAR_PAGESIZE.value(), this.cfgManager.getSystemConfig().getSscMlpagesize());
		}
		
		return template;
	}
	
	/**
	 * 保存用户留言信息
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/user/saveUserMsg.xhtml", method = RequestMethod.POST)
	public String saveUserMsg(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			try {
				BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
				if (sessionUser == null) {
					response.sendRedirect(request.getContextPath() + "/index.xhtml");
					return null;
				}
				
				// 设置请求、响应编码
				setCharacterEncoding(request, response);
				
				// 设置文件上传类型
				String[] suffixs={".jpg",".gif",".png", ".txt", ".zip", ".ppt", ".pdf", ".doc", ".xls"};
				// 设置文件最大大小为1MB(单位：字节)
				int MAX_SIZE_IN_BYTES = 1024 * 1024;  
				// 设置响应状态，默认为成功
				int state = 1;
				
				boolean isMultipart = ServletFileUpload.isMultipartContent(request);
				if (isMultipart) {
					DiskFileItemFactory factory = new DiskFileItemFactory();
					ServletFileUpload upload = new ServletFileUpload(factory);
					List<FileItem> items = upload.parseRequest(request);
					
					Map<String,String> map = new HashMap<String,String>();
					
					for (FileItem item : items) {
						if (item.isFormField()) {
							map.put(item.getFieldName(), item.getString("UTF-8")); 						
						}else{
							String fileName = item.getName();
							long sizeInBytes = item.getSize();
							
							//判断上传文件的类型和大小
							boolean flag = false;
							for (String suffix : suffixs) {
								if (fileName.toLowerCase().endsWith(suffix)) {
									flag = true;
									break;
								}
							}
							
							if (!flag) {
								state = -1;
								break;
							}
							
							if (sizeInBytes > MAX_SIZE_IN_BYTES) {
								state = -2;
								break;
							}
							
							Date now = new Date();
							
							// 生成相对路径
							String pathYear = CommonUtils.getDateTime(now, "yyyy");
							String pathMonth = CommonUtils.getDateTime(now, "MM");
							String xdpath = "/" + pathYear + "/" + pathMonth;
							// 文件保存路径
							String filePath = AppConfig.getRealPath() + AppConfig.getMessageImagePath() + xdpath;
							
							// 创建路径
							File dir = new File(filePath);
							FileUtils.makeDir(dir);
							
							String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
							// 生成新的文件名
							String newFileName = java.util.UUID.randomUUID().toString() + "." + extName;
							
							filePath = filePath + "/" + newFileName;
							File file = new File(filePath);
							item.write(file);
							
							map.put("bumAttachname", newFileName);
							map.put("bumAttachurl", xdpath + "/" + newFileName);
						}
					}
					
					if(state != 1){
						model.put(Constant.VAR_USERMSGSTATE.value(), state);
						return template;
					}
					
					int bumType = Integer.parseInt(map.get("bumType"));
					String bumTitle = map.get("bumTitle");
					String bumContent = map.get("bumContent");
					String bumAttachname = map.get("bumAttachname");
					String bumAttachurl = map.get("bumAttachurl");
					
					BaseUserMessage userMsg = new BaseUserMessage();
					userMsg.setBumName(sessionUser.getBmbNick());
					userMsg.setBumEmail(sessionUser.getBmbEmail());
					userMsg.setBumType(bumType);
					userMsg.setBumTitle(bumTitle);
					userMsg.setBumContent(bumContent);
					userMsg.setBumAttachname(bumAttachname);
					userMsg.setBumAttachurl(bumAttachurl);
					userMsg.setBumIp(request.getRemoteAddr());
					userMsg.setBumTime(new Date());
					userMsg.setBumState(0);
					
					this.umService.saveUserMsg(userMsg);
					
					model.put(Constant.VAR_USERMSGSTATE.value(), state);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		return template;
		
	}
	
	/**
	 * 删除用户留言信息
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/delUserMsg.xhtml", method = RequestMethod.POST)
	public void delUserMsg(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status = "succ";
		String bumId = FrontUtil.getStringParam(request, "bumId", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			this.umService.delUserMessage(bumId);
			this.umService.delUserReMessage(bumId);
		}else{
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 前往会员中心-我的评论
	 * @author liuzhi Feb 18, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/userCmts.xhtml", method = RequestMethod.GET)
	public String userCmts(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			//设置数据
			model.put(Constant.VAR_PAGEINDEX.value(), FrontUtil.getIntegerParam(request, "index", 1));
			model.put(Constant.VAR_PAGESIZE.value(), this.cfgManager.getSystemConfig().getSscClpagesize());
		}
		
		return template;
	}
	
	/**
	 * 删除会员评论信息
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/removeFromUserCmts.xhtml", method = RequestMethod.POST)
	public void removeFromUserCmts(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status = "succ";
		String bucId = FrontUtil.getStringParam(request, "bucId", "");
		
		BaseMemberBase sessionUser = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		if (sessionUser != null) {
			this.ucService.delUserCmt(bucId);
			this.ucService.delUserReplyCmt(bucId);
		}else{
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 获得商品属性列表
	 * @author liuzhi Mar 28, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/user/getGoodsAttrList.xhtml", method = RequestMethod.POST)
	public void getGoodsAttrList(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		setToBeJsonType(request, response);
		
		String goodsId = FrontUtil.getStringParam(request, "goodsId", "");
		
		// 获得商品属性列表
		List gaList = fgaService.getGoodsAttrById(goodsId);
		
		if(gaList !=  null && gaList.size() > 0){
			response.getWriter().write(JSONArray.fromObject(gaList).toString());
		}else{
			response.getWriter().write("");
		}
	}

}
